package example.dawangba.com.z09315222_exp3;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.ToggleButton;

public class Flashlight extends AppCompatActivity {

    private CheckBox checkBox;
    private ImageView imageView;
    private ToggleButton toggle;
    private Switch aSwitch;
    RadioGroup radioGroup;
    RadioButton flashOn;
    RadioButton flashOff;
    MyListener listener  = new MyListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashlight);
        imageView = (ImageView) findViewById(R.id.flash_light_img);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        toggle = (ToggleButton) findViewById(R.id.toggle);
        aSwitch = (Switch) findViewById(R.id.switch_flash);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        flashOn = (RadioButton) findViewById(R.id.radioOpen);
        flashOff = (RadioButton) findViewById(R.id.radioOff);

        checkBox.setOnCheckedChangeListener(listener);

        toggle.setOnCheckedChangeListener(listener);

        aSwitch.setOnCheckedChangeListener(listener);

        radioGroup.setOnCheckedChangeListener(listener);
    }

    private class MyListener implements CompoundButton.OnCheckedChangeListener,RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                imageView.setImageResource(R.mipmap.lampon);
                checkBox.setChecked(true);
                toggle.setChecked(true);
                aSwitch.setChecked(true);
                checkBox.setText("关灯");
                flashOn.setChecked(true);
                flashOff.setChecked(false);
            } else {
                imageView.setImageResource(R.mipmap.lampoff);
                checkBox.setChecked(false);
                toggle.setChecked(false);
                aSwitch.setChecked(false);
                checkBox.setText("开灯");
                flashOff.setChecked(true);
                flashOn.setChecked(false);
            }
        }

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            if (checkedId == flashOn.getId()){
                imageView.setImageResource(R.mipmap.lampon);
                checkBox.setChecked(true);
                toggle.setChecked(true);
                aSwitch.setChecked(true);
                checkBox.setText("关灯");
            } else {
                imageView.setImageResource(R.mipmap.lampoff);
                checkBox.setChecked(false);
                toggle.setChecked(false);
                aSwitch.setChecked(false);
                checkBox.setText("开灯");
            }
        }
    }
}
